package com.cskaoyan.service.mallservice;

import com.cskaoyan.bean.mallbean.KeyWord;
import com.cskaoyan.bean.mallbean.KeyWordExample;
import com.cskaoyan.bo.mallbo.KeyWordBo;
import com.cskaoyan.mapper.mallmapper.KeyWordMapper;
import com.cskaoyan.vo.mallvo.KeyWordListVo;
import com.cskaoyan.vo.mallvo.KeyWordVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author hsp
 * @date 2021/5/10 21:28
 */
@Service
public class KeyWordServiceImpl implements KeyWordService {
    @Autowired
    KeyWordMapper keyWordMapper;
    @Override
    public KeyWordListVo listKeyWord(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        PageHelper.startPage(page,limit);

        KeyWordExample keyWordExample = new KeyWordExample();
        KeyWordExample.Criteria criteria = keyWordExample.createCriteria();
        criteria.andDeletedEqualTo(0);
        if (keyword!=null)
            criteria.andKeywordLike("%"+ keyword+"%");
        if (url!=null)
            criteria.andUrlLike("%"+url+"%");
        keyWordExample.setOrderByClause(sort +" "+ order); //order by add_time desc

        List<KeyWord> keyWords = keyWordMapper.selectByExample(keyWordExample);

        PageInfo<KeyWord> keyWordPageInfo = new PageInfo<>(keyWords);
        int size = keyWordPageInfo.getSize();
        KeyWordListVo keyWordListVo = new KeyWordListVo();
        keyWordListVo.setItems(keyWords);
        keyWordListVo.setTotal(size);

        return keyWordListVo;
    }

    @Override
    public KeyWordVo createKeyWord(String keyword, String url, Integer isHot, Integer isDefault) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        KeyWord keyWord = new KeyWord(
                null,
                keyword,
                url,
                isHot,
                isDefault,
                new Date(),
                new Date(),
                0
        );

        keyWordMapper.insertSelective(keyWord);

        KeyWordVo keyWordVo = new KeyWordVo(
                keyWord.getId(),
                keyWord.getKeyword(),
                keyWord.getUrl(),
                isHot == 1?true:false,
                isDefault == 1?true:false,
                keyWord.getAddTime(),
                keyWord.getUpdateTime()
        );
        return keyWordVo;
    }

    @Override
    public KeyWordBo updateKeyWord(KeyWordBo keyWordBo) {

        keyWordMapper.updateByPrimaryKeySelective(keyWordBo);
        KeyWordBo keyWordBoVo = keyWordMapper.selectByPrimaryKey(keyWordBo.getId());
        return keyWordBoVo;
    }

    @Override
    public void logicDeleteKeyWord(Integer id) {
        keyWordMapper.logicDeleteKeyWord(id);

    }


}
